package com.freesky.web.manager.service.impl;

import com.freesky.common.dao.entity.SystemInfo;
import com.freesky.common.enums.RedisKeyEnum;
import com.freesky.common.enums.ResponseEnum;
import com.freesky.common.exception.MyException;
import com.freesky.common.redis.IRedisTemplate;
import com.freesky.common.utils.reponse.JSONResponse;
import com.freesky.common.utils.reponse.JSONResponseDiretor;
import com.freesky.common.vo.SystemProp;
import com.freesky.web.manager.service.ISystemInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SystemInfoServiceImpl implements ISystemInfoService {
    @Autowired
    private IRedisTemplate redisTemplate;
    @Override
    public SystemInfo getInfo() {
        SystemInfo systemInfo = redisTemplate.getCacheObject(RedisKeyEnum.SYSTEM_INFO.getKey(), SystemInfo.class);
        if (systemInfo == null) {
            systemInfo = new SystemInfo().selectAll().get(0);
            systemInfo.setSystemProp(SystemProp.init());
            redisTemplate.putCache(RedisKeyEnum.SYSTEM_INFO.getKey(), systemInfo);
        }
        return systemInfo;
    }
    @Override
    public JSONResponse updateInfo(SystemInfo info) {
        try {
            info.updateById();
        } catch (Exception e){
            throw new MyException(ResponseEnum.UPDATE_SYSTEM_INFO_ERROR, e, null);
        }
        return JSONResponseDiretor.buildSuccessJSONResponse(null);

    }
}
